﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sheep_Breeding.Infeartructure
{
    public class BasreRepository<T>:IBaseRepository<T> where T : class
    {
        private readonly ILogger<BasreRepository<T>> logger;
        private readonly EFDbContext ctx;

        public BasreRepository(ILogger<BasreRepository<T>> logger,EFDbContext ctx)
        {
            this.logger = logger;
            this.ctx = ctx;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsyn(T t)
        {
            try
            {
                await ctx.Set<T>().AddAsync(t);
                return await ctx.SaveChangesAsync();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Task<int> Batchadd(List<T> t)
        {
            try
            {
                ctx.Set<T>().AddRange(t);
                return ctx.SaveChangesAsync();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Task<int> BatchUpdate(List<T> t)
        {
            try
            {
                ctx.Set<T>().UpdateRange(t);
                return ctx.SaveChangesAsync();

            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Getlist()
        {
            try
            {
                return ctx.Set<T>().AsQueryable();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 根据条件查找信息
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        public async Task<T> GetValue(Expression<Func<T, bool>> exp)
        {
            try
            {
                return await ctx.Set<T>().Where(exp).FirstOrDefaultAsync();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public Task<int> Update(T t)
        {
            try
            {
                ctx.Set<T>().Update(t);
                return ctx.SaveChangesAsync();
            }
            catch (Exception ex)
            {

                logger.LogError("=>" + ex.Message);
                throw ex;
            }
        }
    }
}
